package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.ProjectPlan;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.ProjectPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import com.zzyl.vo.NursingProjectVo;
import com.zzyl.vo.ProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.function.Consumer;

@Service
public class NursingPlanServiceImpl implements NursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private ProjectPlanMapper projectPlanMapper;

    /**
     * 查询所有护理计划
     * @return
     */
    @Override
    public List<NursingPlan> findAll() {
       return nursingPlanMapper.findAll();
    }


    /**
     * 新增护理计划
     * @param nursingPlanDto
     */
    @Override
    @Transactional
    public void add(NursingPlanDto nursingPlanDto) {
        //把dto里的数据拷贝到pojo对象中
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtil.copyProperties(nursingPlanDto,nursingPlan);

        nursingPlan.setUpdateTime(LocalDateTime.now());
        nursingPlan.setCreateTime(LocalDateTime.now());
        //插入数据
        nursingPlanMapper.insert(nursingPlan);
        //获取计划Id
        Integer id = nursingPlan.getId();
        //从dto中获取中间表
        List<ProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        //判断护理项目是否为空
        if(null != projectPlans && projectPlans.size() >0){
            //遍历每一个护理项目并为护理项目的id赋值
            projectPlans.forEach(projectPlan ->{
                projectPlan.setPlanId(id);
                projectPlan.setCreateTime(LocalDateTime.now());
                projectPlan.setUpdateTime(LocalDateTime.now());
            } );
            //批量插入护理计划的护理项目
            projectPlanMapper.batchInsert(projectPlans);
        }
    }

    /**
     * 分页查询数据
     * @param name
     * @param status
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageResponse<NursingPlanVo> getByPage(String name, Integer status, Integer pageNum, Integer pageSize) {
        //设置分页参数
        PageHelper.startPage(pageNum,pageSize);
        //调用方法查看数据
        Page<NursingProjectVo> page = nursingPlanMapper.selectByPage(name,status);
        //封装返回后的结果
        return PageResponse.of(page, NursingPlanVo.class);
    }


    /**
     *
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public NursingPlanVo getInfo(Integer id) {
        //查询plan
        NursingPlan nursingPlan = nursingPlanMapper.getInfo(id);
        //将实体类中的数据拷贝到vo
        NursingPlanVo bean = BeanUtil.toBean(nursingPlan, NursingPlanVo.class);
        //查询护理项目project
        List<ProjectPlanVo> projectPlans = projectPlanMapper.getById(id);
        //将集合放到vo类中
        bean.setProjectPlans(projectPlans);
        return bean;
    }

    /**
     * 修改护理计划
     * @param nursingPlanDto
     * @param id
     */
    @Override
    public void update(NursingPlanDto nursingPlanDto, Integer id) {
        //修改护理计划
        NursingPlan nursingPlan = new NursingPlan();
        //把dto得数据拷贝到pojo中
        BeanUtil.copyProperties(nursingPlanDto,nursingPlan);
        nursingPlan.setId(id);
        nursingPlan.setUpdateTime(LocalDateTime.now());
        nursingPlan.setCreateTime(LocalDateTime.now());
        nursingPlanMapper.update(nursingPlan);
        //删除中间表信息
        projectPlanMapper.batchDel(id);
        //重新向中间表中插入数据
        List<ProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();

        //遍历中间表设置planid和时间
        projectPlans.forEach(new Consumer<ProjectPlan>() {
            @Override
            public void accept(ProjectPlan projectPlan) {
                projectPlan.setPlanId(id);
                projectPlan.setUpdateTime(LocalDateTime.now());
                projectPlan.setCreateTime(LocalDateTime.now());
            }
        });
        projectPlanMapper.batchInsert(projectPlans);
    }

    /**
     * 根据id删除护理计划和项目
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        nursingPlanMapper.batchDel(id);
        projectPlanMapper.batchDel(id);
    }

    /**
     * 启用和禁用
     * @param id
     * @param status
     */
    @Override
    public void updateStatus(Integer id, Integer status) {
        NursingPlan nursingPlan = new NursingPlan();
        nursingPlan.setId(id);
        nursingPlan.setStatus(status);
        nursingPlanMapper.updateStatus(nursingPlan);
    }
}
